package com.icetec.iceintel.ui.activity;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.icetec.R;
import com.icetec.iceintel.framework.IceBaseActivity;
import com.icetec.iceintel.model.Alarm;

/**
 * Created by amo on 2015/2/18.
 * 闹钟弹出界面
 * 测试中 未完成
 */
public class AlarmAlertActivity extends IceBaseActivity {

    private Alarm alarm;
    private MediaPlayer mediaPlayer;
    private Vibrator vibrator;
    TextView alarmText;
    Button cancelBtn;
    TextView userAlarmTitleTv;
    TextView alrarmExplainTv;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        setContentView(R.layout.activity_alarm_alert);



        Bundle bundle = this.getIntent().getExtras();
        alarm = (Alarm) bundle.getSerializable("alarm");

        alarmText=(TextView)findViewById(R.id.alarmAlertText_tv);
        cancelBtn=(Button)findViewById(R.id.alarm_alert_cancel_btn);
        userAlarmTitleTv=(TextView)findViewById(R.id.alarmAlertText_user_tips);
        alrarmExplainTv=(TextView)findViewById(R.id.alarm_alert_explain);

        this.setTitle(alarm.getAlarmName());
        userAlarmTitleTv.setText(alarm.getAlarmName());
        userAlarmTitleTv.setVisibility(View.INVISIBLE);


        switch (alarm.getAlarmType()) {
            case AWAKE:
                alarmText.setText(R.string.alarm_awake);
                alrarmExplainTv.setText(R.string.awake_alarm_explain);

                break;
         //   case SCHEDULE:
        //        alarmText.setText(R.string.alarm_awake);
         //       userAlarmTitleTv.setText(R.string.awake_alarm_explain);
       //         break;
            case SLEEP:
                alarmText.setText(R.string.alarm_sleep);
                alrarmExplainTv.setText(R.string.sleep_alarm_explain);
                break;
        }



        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);

        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                switch (state) {
                    case TelephonyManager.CALL_STATE_RINGING:
                        Log.d(getClass().getSimpleName(), "Incoming call: "
                                + incomingNumber);
                        try {
                            mediaPlayer.pause();
                        } catch (IllegalStateException e) {

                        }
                        break;
                    case TelephonyManager.CALL_STATE_IDLE:
                        Log.d(getClass().getSimpleName(), "Call State Idle");
                        try {
                            mediaPlayer.start();
                        } catch (IllegalStateException e) {

                        }
                        break;
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };

        telephonyManager.listen(phoneStateListener,
                PhoneStateListener.LISTEN_CALL_STATE);

        startAlarm();

    }

    @Override
    public void onResume() {
        super.onResume();

    }



    private void startAlarm() {

        if (alarm.getAlarmTonePath() != "") {
            mediaPlayer = new MediaPlayer();
            if (alarm.getVibrate()) {
                vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                long[] pattern = { 1000, 200, 200, 200 };
                vibrator.vibrate(pattern, 0);
            }
            try {
                mediaPlayer.setVolume(1.0f, 1.0f);
                mediaPlayer.setDataSource(this,
                        Uri.parse(alarm.getAlarmTonePath()));
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                mediaPlayer.setLooping(true);
                mediaPlayer.prepare();
                mediaPlayer.start();

            } catch (Exception e) {
                mediaPlayer.release();

            }
        }

    }

    /*
     * (non-Javadoc)
     *
     * @see android.app.Activity#onBackPressed()
     */
    @Override
    public void onBackPressed() {

            super.onBackPressed();
            StaticWakeLock.lockOff(this);
    }

    /*
     * (non-Javadoc)
     *
     * @see android.app.Activity#onPause()
     */
    @Override
    public void onPause() {
        super.onPause();
        StaticWakeLock.lockOff(this);
    }

    @Override
    protected void onDestroy() {
        StaticWakeLock.lockOff(this);
        try {
            if (vibrator != null)
                vibrator.cancel();
        } catch (Exception e) {

        }
        try {
            mediaPlayer.stop();
        } catch (Exception e) {

        }
        try {
            mediaPlayer.release();
        } catch (Exception e) {

        }
        super.onDestroy();
    }





}
